package com.micro.api.order.service.impl;

import com.micro.api.entity.User;
import com.micro.api.service.IOrderService;
import com.micro.base.BaseApiService;
import com.micro.base.ResponseBase;
import com.micro.feign.IUserServiceFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @title: OrderServiceImpl
 * @Author: yzp
 * @Date: 2019/9/28 11:12
 * @Version 1.0
 */
@RestController
public class OrderServiceImpl extends BaseApiService implements IOrderService {

    /**
     * 订单服务继承会员服务接口，用来实现feign客户端 减少重复接口代码
     */
    @Autowired
    private IUserServiceFeign uerServiceFeign;


    @RequestMapping("/orderToUser")
    @Override
    public String orderToUser(String name) {
        User user = uerServiceFeign.getUser(name);
        return "订单微服务==="+user.toString();
    }

    @Override
    @RequestMapping("/orderToUserInfo")
    public ResponseBase orderToUserInfo() {
        ResponseBase res = uerServiceFeign.getUserInfo();
        return res;
    }

    @RequestMapping("/getOrder")
    public String getOrder(){
        return "我的订单微服务";
    }

    // 解决服务雪崩效应
    // fallbackMethod 方法的作用：服务降级执行
    // @HystrixCommand 默认开启线程池隔离方式,服务降级,服务熔断
    // 设置Hystrix服务超时时间
    /**
     * @HystrixCommand<br>
     * 					默认开启服务隔离方式 以线程池方式<br>
     *                     默认开启服务降级执行方法orderToMemberUserInfoHystrixFallback<br>
     *                     默认开启服务熔断机制<br>
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "orderToUserInfoHystrixFallback")
    @RequestMapping("/orderToUserInfoHystrix")
    public ResponseBase orderToMemberUserInfoHystrix() {
        System.out.println("orderToUserInfoHystrix:" + "线程池名称:" + Thread.currentThread().getName());
        return uerServiceFeign.getUserInfo();
    }

    public ResponseBase orderToUserInfoHystrixFallback() {
        return setResultSuccess("返回一个友好的提示：服务降级,服务器忙，请稍后重试!");
    }

    // 订单服务接口
    @RequestMapping("/orderInfo")
    @Override
    public ResponseBase orderInfo() {
        System.out.println("orderInfo:" + "线程池名称:" + Thread.currentThread().getName());
        return setResultSuccess();
    }

}
